home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
preccx
/
prccx240.lha
/
fib.y
< prev
next >
Wrap
Text File
|
1993-05-28
|
1KB
|
77 lines
/* FIBONNACI - an example parser (usage at the end of this file)
I suggest you
a) run
preccx fib.y fib.c
b) compile
gcc -ansi -o fib fib.c -L . -lcc -D_STDC_
^^^^^
c) run
fib
I have seen slightly different incantations for the compiler in
different OS's, and I am supposing that you put the libcc.a that
comes with preccx in this directory. You may need some more flags
which specify your target machine type (e.g. -D__hp9000s300).
*/
# define TOKEN char
# define VALUE char*
# define BEGIN call_mode=1;printf("hi!\n");
# include "ccx.h"
# define INT(x) (int)(x)
char shortbuffer[32];
int shortnum;
PARAM longnum;
# define FIRSTDIGIT(n) (0==n)?(PARAM)0:\
(sprintf(shortbuffer,"%d",(int)n),\
shortbuffer[1]=0,\
sscanf(shortbuffer,"%d",&shortnum),\
longnum=shortnum\
)
# define LASTDIGITS(n) (0==n)?(PARAM)0:\
(sprintf(shortbuffer,"%d",(int)n),\
sscanf(&shortbuffer[1],"%d",&shortnum),\
longnum=shortnum\
)
# define TOTL printf("%d terms OK\nNext terms are %d,%d,..\n",n,a,b)
MAIN(fibber)
@fibber = fibs*
@fibs = fib((PARAM)1,(PARAM)1,(PARAM)0) $!
@fib(a,b,n) = number(a) <','> fib(b,a+b,n+(PARAM)1)
@ | <'.'> <'.'> :TOTL;:
@number(n)= digit(n)
@ | digit(FIRSTDIGIT(n)) number(LASTDIGITS(n))
@digit(n) = <n+'0'> :/* digit rep of small n */:
/* ------------------ Example inputs: ----------------
1,1,2,3,5,..
(OK)
1,1,2,3,5,8,13,21,..
(OK)
1,1,2,3,5,8,13,21,34,51,85,..
error: failed parse: probable error at <>1,85,..
*/